<?php
App::uses('AppController', 'Controller');
/**
 * Offices Controller
 *
 * @property Office $Office
 */
class OfficesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
	
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		$user = $this->Auth->user();   
		$this->Office->recursive = 0;
		$this->set('offices', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if (!$this->Office->exists($id)) {
			throw new NotFoundException(__('Invalid office'));
		}
		$options = array(
			'conditions' => array('office.' . $this->Office->primaryKey => $id), 'recursive' => 2 //array of conditions
		);
		$this->set('office', $this->Office->find('first', $options));
		
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if ($this->request->is('post')) {
			$this->Office->create();
			if ($this->Office->save($this->request->data)) {
				$this->Session->setFlash(__('The office has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The office could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if (!$this->Office->exists($id)) {
			throw new NotFoundException(__('Invalid office'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Office->save($this->request->data)) {
				$this->Session->setFlash(__('The office has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The office could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Office.' . $this->Office->primaryKey => $id));
			$this->request->data = $this->Office->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		$this->Office->id = $id;
		if (!$this->Office->exists()) {
			throw new NotFoundException(__('Invalid office'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Office->delete()) {
			$this->Session->setFlash(__('Office deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Office was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
